package com.ljc.webview.slice;

import com.ljc.webview.MainAbility;
import com.ljc.webview.ResourceTable;
import com.ljc.webview.utils.PreferenceUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.webengine.*;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;
import ohos.media.image.PixelMap;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
        webView.getWebConfig().setJavaScriptPermit(true);  // 如果网页需要使用JavaScript，增加此行；如何使用JavaScript下文有详细介绍
        final String url = "http://47.112.192.216/"; // EXAMPLE_URL由开发者自定义
        webView.load(url);
        // 存储token
        PreferenceUtils.putToken(this,"token form HarmonyOS");

        webView.addJsCallback("getTokenForHarmonyOS", new JsCallback() {
            @Override
            public String onCallback(String msg) {
                // 增加自定义处理
                return PreferenceUtils.getToken(getContext());
            }
        });

        // 分享页面
        webView.addJsCallback("sharePage", new JsCallback() {
            @Override
            public String onCallback(String msg) {
                List<DeviceInfo> onlineDevices = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);
                // 判断组网设备是否为空
                if (!onlineDevices.isEmpty()) {
                    Intent intent1 = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId(onlineDevices.get(0).getDeviceId())
                            .withBundleName(getBundleName())
                            .withAbilityName(MainAbility.class.getName())
                            .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE)
                            .build();
                    intent1.setOperation(operation);
                    startAbility(intent1);
                    return "拉起成功";
                }else {
                    return "未找到设备";
                }
            }
        });

        webView.setWebAgent(new WebAgent() {
            @Override
            public void onLoadingPage(WebView webview, String url, PixelMap favicon) {
                super.onLoadingPage(webview, url, favicon);
                // 页面开始加载时自定义处理
            }

            @Override
            public void onPageLoaded(WebView webview, String url) {
                super.onPageLoaded(webview, url);
                // 页面加载结束后自定义处理
            }

            @Override
            public void onLoadingContent(WebView webview, String url) {
                super.onLoadingContent(webview, url);
                // 加载资源时自定义处理
            }

            @Override
            public void onError(WebView webview, ResourceRequest request, ResourceError error) {
                super.onError(webview, request, error);
                // 发生错误时自定义处理
            }
        });
    }



    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
